package com.example.elastcisearch;

import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.junit.Before;
import org.junit.Test;

public class TestDelete {
    private RestHighLevelClient client = null;


    @Before
    public void before() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.228.104", 9200, "http"));
        client = new RestHighLevelClient(builder);
    }
    
    @SneakyThrows
    @Test
    public void delete() {
        DeleteRequest request = new DeleteRequest("shop", "abc");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.getIndex());
        System.out.println(response.getResult());
    }


    @SneakyThrows
    @Test
    public void deleteByQuery() {
        DeleteByQueryRequest request = new DeleteByQueryRequest("order");
        request.setQuery(QueryBuilders.termsQuery("orderNo", "rggregtrg545t44"));
        BulkByScrollResponse response = client.deleteByQuery(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }





}
